package com.zuqiuzhandui.liveing.view.choose;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.zuqiuzhandui.R;
import com.zuqiuzhandui.liveing.base.BaseActivity;
import com.zuqiuzhandui.liveing.common.CheckParameter;
import com.zuqiuzhandui.liveing.common.UserInfomationManage;
import com.zuqiuzhandui.liveing.model.resultModel.LiveCreateRs;
import com.zuqiuzhandui.liveing.model.resultModel.TeamListRs;
import com.zuqiuzhandui.liveing.model.resultModel.TeamScheduleRs;
import com.zuqiuzhandui.liveing.util.UpdataUtil;
import com.zuqiuzhandui.liveing.view.AboutActivity;
import com.zuqiuzhandui.liveing.view.chooseSchedule.ChooseScheduleActivity;
import com.zuqiuzhandui.liveing.view.chooseTeam.ChooseTeamActivity;
import com.zuqiuzhandui.liveing.view.main.MainActivity;
import com.zuqiuzhandui.liveing.view.streaming.StreamingActivity;
import com.zuqiuzhandui.testing.myhelper.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ChooseActivity extends BaseActivity implements ChooseContract.View {

    @BindView(R.id.tv_team)
    TextView tvTeam;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_turn)
    TextView tvTurn;
    @BindView(R.id.tv_team_a)
    TextView tvTeamA;
    @BindView(R.id.tv_team_b)
    TextView tvTeamB;
    @BindView(R.id.activity_choose)
    LinearLayout activityChoose;
    @BindView(R.id.ll_schedule)
    LinearLayout llSchedule;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.ll_team_item)
    LinearLayout llTeamItem;

    @BindView(R.id.ll_item_schedule)
    LinearLayout llItemSchedule;
    @BindView(R.id.in_empty_schedule)
    LinearLayout llEmptySchedule;


    private IWXAPI api;
    private ChooseContract.Presenter presenter;
    private ArrayList<TeamListRs> teamLists;
    private ArrayList<TeamScheduleRs> teamSchedules;
    private int teamIndex = -1;
    private int scheduleIndex = -1;

    @Override
    public void setViewId() {
        setContentView(R.layout.activity_choose);
        setNeedDisplayHomeAsUpEnabled(false);
    }


    @OnClick({R.id.tv_team, R.id.ll_schedule, R.id.submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_team:
                //选择球队
                Intent intent = new Intent(this, ChooseTeamActivity.class);
                Bundle teambundle = new Bundle();
                teambundle.putParcelableArrayList("team", teamLists);
                intent.putExtra(BUNDLE_KEY, teambundle);
                startActivityForResult(intent, 0);
                break;
            case R.id.ll_schedule:
                //选择日程
                Intent intent1 = new Intent(this, ChooseScheduleActivity.class);
                Bundle scheduleBundle = new Bundle();
                scheduleBundle.putSerializable("schedule", teamSchedules);
                intent1.putExtra(BUNDLE_KEY, scheduleBundle);
                startActivityForResult(intent1, 1);
                break;
            case R.id.submit:
                String errorStr = CheckParameter.getInstance().checkTeamAndSchedule(teamIndex, scheduleIndex);
                if (errorStr != null) {
                    showTip(errorStr);
                    break;
                }
                String teamCode = teamLists.get(teamIndex).getCode();
                String typeCode = teamSchedules.get(scheduleIndex).getCode();
                String liveType = teamSchedules.get(scheduleIndex).getScheduleType();
                presenter.requestLiveCreate(teamCode, typeCode, liveType);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 0:
                //选择球队的回调
                if (resultCode == RESULT_OK) {
//                    ToastUtils.showToast(ChooseActivity.this, "选择了球队");
                    teamIndex = data.getBundleExtra(BUNDLE_KEY).getInt("index");
                    TeamItem.setItem(this, teamLists.get(teamIndex), llTeamItem);
                    presenter.requestTeamSchedule(teamLists.get(teamIndex).getCode());
                }
                break;
            case 1:
                //选择日程的回调
                if (resultCode == RESULT_OK) {
//                        ToastUtils.showToast(ChooseActivity.this, "选择了日程");
                    scheduleIndex = data.getBundleExtra(BUNDLE_KEY).getInt("index");
                    ScheduleItem.setItemView(teamSchedules.get(scheduleIndex), llSchedule);
                }
                break;
        }
    }

    @Override
    public void initView() {
        regToWx();
        setBackEnable(false);
        presenter = new ChoosePresenter(this);
        UpdataUtil.CheckUpdate(this);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
//            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
//        }

        setCouldDoubleBackExit(true);
    }


    @Override
    protected void initToolbar() {
        super.initToolbar();
//        getToolbar().inflateMenu(R.menu.menu_choose_activity);
//        getToolbar().setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                switch (item.getItemId()) {
//                    case R.id.item_about:
////                        ToastUtils.showToast(ChooseActivity.this,"点击了关于我们");
//                        toLastView(AboutActivity.class);
//                        break;
//                    case R.id.item_quit:
//                        UserInfomationManage.removeUser();
//                        Intent intent = new Intent(ChooseActivity.this, MainActivity.class);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//                        startActivity(intent);
//                        break;
//                }
//                return true;
//            }
//        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_choose_activity, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_about:
//                        ToastUtils.showToast(ChooseActivity.this,"点击了关于我们");
                toLastView(AboutActivity.class);
                break;
            case R.id.item_quit:
                UserInfomationManage.removeUser();
                Intent intent = new Intent(ChooseActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 注册到微信
     */
    private void regToWx() {
        api = WXAPIFactory.createWXAPI(getApplicationContext(), WEIXIN_APP_ID, true);
        api.registerApp(WEIXIN_APP_ID);
    }

    @Override
    public void initData() {
        presenter.requestTeam();
    }

    @Override
    public void showSuccessTeam(List<TeamListRs> dataList) {
        if (dataList == null || dataList.isEmpty()) {
            showEmptyTeamView();
        }
        teamLists = new ArrayList<>();
        teamLists.addAll(dataList);
        String code = dataList.get(0).getCode();
        presenter.requestTeamSchedule(code);
        setTeamView(dataList);
    }

    /**
     * 没有查找到球队
     */
    private void showEmptyTeamView() {
        ToastUtils.showToast(this, "该球队没有比赛日程");
    }


    private void setTeamView(List<TeamListRs> dataList) {
        TeamItem.setItem(this, dataList.get(0), llTeamItem);
        teamIndex = 0;
    }


    @Override
    public void showSuccessTeamSchedule(List<TeamScheduleRs> dataList) {
        teamSchedules = new ArrayList<>();
        if (dataList == null || dataList.isEmpty()) {
//            ToastUtils.showToast(this,"该球队没有比赛日程");
//            ScheduleItem.setItemView(null, llSchedule);
            showEmptyScheduleView();
            return;
        }
        teamSchedules.addAll(dataList);
        scheduleIndex = 0;
        ScheduleItem.setItemView(dataList.get(0), llSchedule);
    }

    private void showEmptyScheduleView() {
        llItemSchedule.setVisibility(View.GONE);
        llEmptySchedule.setVisibility(View.VISIBLE);
    }

    @Override
    public void showSuccessLiveCreate(LiveCreateRs data) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(PARCELABLE_KEY, data);
        Intent intent = new Intent(this, StreamingActivity.class);
        intent.putExtra(BUNDLE_KEY, bundle);
        startActivity(intent);
    }


    @Override
    public void setPresenter(ChooseContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public boolean isActive() {
        return isActive;
    }


    @Override
    public void showTip(String message) {
        ToastUtils.showToast(this, message);
    }


    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putParcelableArrayList("teamList", teamLists);
        outState.putSerializable("schedule", teamSchedules);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        teamLists = savedInstanceState.getParcelableArrayList("teamList");
        teamSchedules = (ArrayList<TeamScheduleRs>) savedInstanceState.getSerializable("schedule");
    }
}
